/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.cardapiodigital.database;

import br.com.cardapiodigital.exception.CardapioDigitalException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author egberto
 */
public abstract class ConnectionManager {

    private static final String STR_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=cardapios";
    private static final String USER = "sa";
    private static final String PASSWORD = "pomarola";
        
    public static Connection getConexao() throws CardapioDigitalException {

        Connection conn = null;

        try {
            Class.forName(STR_DRIVER);

            conn = DriverManager.getConnection(URL, USER, PASSWORD);
            System.out.println("conexao com sucesso");
            return conn;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Driver nao encontrado";
            throw new CardapioDigitalException(errorMsg, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).
                    log(Level.SEVERE, null, ex);
            String errorMsg = "Erro ao obter a conexao";
            throw new CardapioDigitalException(errorMsg, ex);
        }
        
    }

    public static void close(Connection conn) throws CardapioDigitalException {
        try {
            if (conn != null) {
                conn.close();
                System.out.println("conexao fechada");
            }
        } catch (SQLException ex) {
            String errorMsg = "Nao foi possivel fechar a conexao com o banco";
            throw new CardapioDigitalException(errorMsg, ex);
        }
    }
    
    public static void close(Connection conn, Statement stmt) throws CardapioDigitalException{
        try{
            if(stmt != null){
                stmt.close();
                System.out.println("statement fechada");
            }
        }catch(SQLException ex){
            String errorMsg = "Nao foi possivel fechar o statement com o banco";
            throw new CardapioDigitalException(errorMsg, ex);
        }
        ConnectionManager.close(conn);
    }
    
    public static void close(Connection conn, Statement stmt, ResultSet rs)
                    throws CardapioDigitalException{
        try{
            if(rs != null){
                rs.close();
                System.out.println("resultset fechada");
            }
        }catch(SQLException ex){
            String errorMsg = "Nao foi possivel fechar o resultset com o banco";
            throw new CardapioDigitalException(errorMsg, ex);            
        }
        ConnectionManager.close(conn,stmt);
    }
    
   
}
